package com.yiwei.zhh.api.requestInfo;

import com.yiwei.zhh.api.Api;
import com.yiwei.zhh.api.bean.UpdateHeadBean;
import com.yiwei.zhh.api.bean.UploadImageBean;
import com.yiwei.zhh.api.callback.MyPsersonalContract;
import com.yiwei.zhh.util.Network;
import com.yiwei.zhh.util.RxLifeUtil;
import com.yiwei.zhh.util.ToastUtil;

/**
 * Created by Administrator on 2017/5/14.
 */

public class MyPersonalRequest extends BaseRequest<MyPsersonalContract.View> implements MyPsersonalContract.Presenter<MyPsersonalContract.View> {

    public static MyPersonalRequest create() {
        return new MyPersonalRequest();
    }


    @Override
    public void logout() {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        Api.api_service.getLogoutInfo().compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.logoutSuccess(result);
                });
    }

    @Override
    public void updateHead(String img) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        UploadImageBean bean = new UploadImageBean();
        bean.img = img;
        Api.api_service.uploadImg(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.updateHeadSuccess(result);
                });
    }

    @Override
    public void uploadAddress(String photo) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        UpdateHeadBean bean = new UpdateHeadBean();
        bean.photo = photo;
        Api.api_service.updatehead(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.uploadAddressSuccess();
                });
    }
}
